(function ($) {
	defaultcookiesinput = function(o){
		o = $.extend({
			store           : "1",
			buynow          : "1",
			availability    : "1",
			imageFlag       : "1"
		}
		, o||{});
		if($.cookie("defaultsetting")==null){
			$.cookie("defaultsetting","1",options);
			$.cookie("expandstore","1",options);
			$.cookie("expandbuynow","1",options);
			$.cookie("expandavailability","1",options);
			$.cookie("expandimageFlag","1",options);
			$.cookie("shopping","1",options);
			$.cookie(COOKIE_NAME,"", options);
		}
		for(var key in o){
			if(o[key]==""){
				$.cookie("expand"+key,"1",options);
			}
			else{
				$.cookie("expand"+key,"",options);
			}
		}
	};
	defaultcookiesselect = function(o){
		o = $.extend({
			item_status   : "",
			Gift_icon     : "",
			search_area   : ""
		}
		, o||{});
		for(var key in o){
			//alert(o[key]);
			$.cookie("expand"+key,o[key],options);
		}
	};
	siteclickhistory = function(){
		//defalut
		if(search_type==""){
			search_type = "auction";
			if(search_site==""){
				search_site="yahoo,bidders";
			}
		}
		else if(search_type=="shopping"&&search_site==""){
			search_site="yahoo,bidders,rakuten,amazon,kakaku";
		}
		//input:checkbox
		checkcookie("input:checkbox[data-checkbox]","data-checkbox");
		//select
		checkcookiefotrselect("data-select");
		//input:radio
		$("#auction,#shopping").click(function(){
			select_aucORshop(this);
		});
		checkcookie("input:radio","id");
		//if($.cookie("shopping")=="1"){
		if(search_type == "auction"){
			//search_type = "auction";
			$("#auction").click();
			$.cookie("shopping","1",options);
			$.cookie("auction","",options);
		}
		else{
			//search_type = "shopping";
			$("#shopping").click();
			$.cookie("shopping","",options);
			$.cookie("auction","1",options);
		}
	};
	checkcookie = function(s,a){
		$(s).each(function(){
			var thisa=$(this).attr(a);
			$(this).change(function(){
				if(thisa=="auction"||thisa=="shopping"){
					$.cookie("auction","1",options);
					$.cookie("shopping","1",options);
					select_aucORshop(this);
					//alert(thisa);
				}
				var attr_checked = $(this).attr("checked");
				$.cookie(thisa,attr_checked==true?"":"1",options);
				setsitedisable(thisa,attr_checked);
				if(a.indexOf("data-")==-1){
					GetSites();
					resetcategory();
				}
			});
			var sitecookie = $.cookie(thisa);
			if(sitecookie=="1"){
				$(this).attr("checked",false);
			}
			else{
				$(this).attr("checked",true);
			}
			////////////////////////////////
			if(thisa==search_type+$(this).val()){
				$(this).attr("checked",false);
				if(search_site.indexOf($(this).val())!=-1){
					$(this).attr("checked",true);
				}
			};
			////////////////////////////////
		
		});
	};
	setsitedisable = function(attr,status){
      var obj=false;
      var site_status = !!status;
      var site=false;
      if(attr.indexOf("expand")!=-1){
          attr = attr.replace("expand","");
          if(attr=="imageFlag"){
              //obj = "input[data-checkbox='shoppingrakuten']";
              obj = "input[data-checkbox='shoppingamazon']";
              obj += ",input[data-checkbox='shoppingkakaku']";
              obj += ",input[data-checkbox='shoppingbidders']";
              obj += ",input[data-checkbox='shoppingyahoo']";
              if(status){
                  obj += ",input[data-checkbox='expandavailability']";
              }
              site = "rakuten";
          }
          else if(attr=="availability"){
              obj = "input[data-checkbox='shoppingrakuten']";
              obj += ",input[data-checkbox='shoppingamazon']";
              obj += ",input[data-checkbox='shoppingkakaku']";
              obj += ",input[data-checkbox='shoppingbidders']";
              if(status){
                  obj += ",input[data-checkbox='expandimageFlag']";
              }
              //obj = ",input[data-checkbox='shoppingyahoo']";
              site = "yahoo";
          }
          else{
              site_status = false;
              $("#expand_auction").find("input:checkbox").each(function(){
                  if($(this).attr("checked")==true){
                      site_status = true;
                  };
              });
              $("#expand_auction").find("select").each(function(){
                  if($(this).val()!=""){
                      site_status = true;
                  };
              });
              obj = "input[data-checkbox='auctionbidders']";
              site = "yahoo";
          }
      }
      GetSites();
      $(obj).attr("disabled",site_status);
      if(site_status){
          $(obj).attr("checked",!site_status);
      }
      if(site!=false&&site_status==true){
          $("#search_input_site").val(site);
      }
      //alert($("#search_input_site").val());
	};
	checkcookiefotrselect = function(a){
		$("select["+a+"]").each(function(){
			var thisa=$(this).attr(a);
			$(this).change(function(){
				$.cookie(thisa,$(this).val(),options);
				setsitedisable(thisa,$(this).val());
			});
			var sitecookie = $.cookie(thisa);
			//alert(sitecookie);
			if(sitecookie!=null){
				$(this).find("option").each(function(){
					if($(this).val()==sitecookie){
						$(this).attr("selected",true);
					}
				});
			}
		});
	};
	resetSortLink = function(){
    $("#ninki").attr("href","javascript:Fastsortby('bids,D');").html(search_interface["_search_Popular"]);
    $("#price").attr("href","javascript:Fastsortby('price,D');").html(search_interface["_search_Price"]);
    $("#nogori").attr("href","javascript:Fastsortby('end,D');").html(search_interface["_search_Remainingtime"]);
	};
	GetSorbyCookie = function(){
    resetSortLink();
		var sortby = $.cookie("sortby");
		if(sortby!=""&&sortby!=null){
			$("#Show_sortby").val(sortby);
			var upimg = "<img class=\"bg hyouji_up_down _up\" src=\"/template/images/clear.gif\"></img>";
			var downimg = "<img class=\"bg hyouji_up_down _down\" src=\"/template/images/clear.gif\"></img>";
			if(sortby == "bids,D"){
				$("#ninki").attr("href","javascript:Fastsortby('bids,A');").html(upimg+search_interface["_search_Popular"]);
			}
			else if(sortby == "bids,A"){
				$("#ninki").attr("href","javascript:Fastsortby('bids,D');").html(downimg+search_interface["_search_Popular"]);
			}
			else if(sortby == "price,D"){
				$("#price").attr("href","javascript:Fastsortby('price,A');").html(upimg+search_interface["_search_Price"]);
			}
			else if(sortby == "price,A"){
				$("#price").attr("href","javascript:Fastsortby('price,D');").html(downimg+search_interface["_search_Price"]);
			}
			else if(sortby == "end,D"){
				$("#nogori").attr("href","javascript:Fastsortby('end,A');").html(upimg+search_interface["_search_Remainingtime"]);
			}
			else if(sortby == "end,A"){
				$("#nogori").attr("href","javascript:Fastsortby('end,D');").html(downimg+search_interface["_search_Remainingtime"]);
			}
		}
	};
	/////////////////////////////////////////
	SetHistory = function(objid,isAddhistory){
		var cookies_temp = ($.cookie(COOKIE_NAME)==null)?"":$.cookie(COOKIE_NAME);
		var cookies_temp_num = 0;
		var datakey = $("#items_"+objid).attr("data-key");
		if(isAddhistory=="yes"){
			var itemdata = NowItemsTemp[objid];
			var item_title = itemdata.title;
			if(item_title==""||item_title==null||item_title==undefined){
				item_title = $("#items_"+objid).find(".item_title").text();
			}
			if($.browser.msie){
				item_title = escape(item_title);//encodeURI(item_title);
			}
			//alert(item_title);
			var querystring = "site="+itemdata.site+"&sType="+search_type+"&title="+itemdata.title+"&itemid="+itemdata.itemid+"&img="+itemdata.img+"&price="+itemdata.price+"&store="+itemdata.store+"&endtime="+itemdata.endtime+"&itemkey="+objid+"&";
			if(cookies_temp.indexOf(datakey)==-1&&datakey!=undefined){
				cookies_temp+=","+datakey;
				cookies_temp_num = cookies_temp.match(/,/g).length-17;
				for(i=0;i<cookies_temp_num;i++){
					cookies_temp = cookies_temp.substring(cookies_temp.indexOf(",")+1);
				}
				$.cookie(COOKIE_NAME,cookies_temp, options);
				$.ajax({
					type: "get",
					url: "./WebService/search.php?dom=JSON&type=history&"+querystring+Math.random()*10000000000,
					success: function(msg){
						//$.cookie(COOKIE_NAME,cookies_temp, options);
						GetHistory();
					}
				});
			}
		}
	};
	GetHistory = function(){
		var cookies_temp = ($.cookie(COOKIE_NAME)==null)?"":$.cookie(COOKIE_NAME);
		var sites = "",itemids="";
		if(cookies_temp!=""){
			//$("#searchhistory").show();
			historyitem = cookies_temp.split(',');
			for (key in historyitem){
				if(historyitem[key]!=null&&historyitem[key]!=""&&historyitem[key]!="null"&&historyitem[key]!="null|undefined"){
					sitecookies = historyitem[key].split('|');
					if(sites!=""){
						sites+=","
					}
					if(itemids!=""){
						itemids+=","
					}
					sites+=sitecookies[0];
					itemids+=sitecookies[1];
				}
			}
			querystring = "site="+sites+"&itemid="+itemids+"&";
			$.ajax({
				type: "get",
				url: "./WebService/search.php?dom=JSON&type=gethistory&"+querystring+Math.random()*10000000000,
				dataType: "json",
				success: function(msg){
					//for(jsonitem in msg){
					//  alert(msg[jsonitem].title);
					//}
					putdataindiv(msg);
				}
			});
		}
		GetSorbyCookie();
	};
	putdataindiv = function(jsondata){
		var resulthtml = "<div class=\"items\" id=\"items_##itemkey#\" data-history=\"1\" data-key=\"##site#|##itemid#\" onclick=\"ShowItem('##itemkey#','##site#','##itemid#','##sType#','no')\"><a><div class=\"item_sites item_site_##site#\"></div><div class=\"item_img\"><img src=\"##img#\" alt=\"##title#\" class=\"item_main_images\"/></div><div class=\"item_title\">##title#</div></a><div class=\"item_info\"><div class=\"item_price\">##price#</div><div class=\"item_store\">##store#</div></div></div>";
		$("#search_history").empty();
		/////////////////////////////////////////////
		$("#search_history").html('<div class="jCarouselLite"><ul class="jCarouselLiteul"></ul></div>');
		/////////////////////////////////////////////
		
		var addhtml="",itemendtime = "",storename="",jCarouselLiteulhtml="";
		var hidedivnum = 0;
		var displayval = "inline";
		var historybox_html = "";
		var realdata_length = 0;
		for(jsonitem in jsondata){
			if($.browser.msie||jsondata[jsonitem].title==null||jsondata[jsonitem].title==""||jsondata[jsonitem].title==undefined){
				reGetHistorytitle(jsondata[jsonitem].site,jsondata[jsonitem].sType,jsondata[jsonitem].itemid,jsondata[jsonitem].itemkey);//encodeURI(item_title);
			}
			//不重复才添加
			if(jCarouselLiteulhtml.indexOf(jsondata[jsonitem].itemid)==-1&&jsondata[jsonitem].itemkey!=null){
				realdata_length++;
				addhtml = resulthtml.replace(/##site#/g,jsondata[jsonitem].site);
				//addhtml = addhtml.replace(/##none#/g,displayval);
				addhtml = addhtml.replace(/##sType#/g,jsondata[jsonitem].sType);
				addhtml = addhtml.replace(/##itemid#/g,jsondata[jsonitem].itemid);
				addhtml = addhtml.replace(/##title#/g,jsondata[jsonitem].title);
				addhtml = addhtml.replace(/##img#/g,jsondata[jsonitem].img);
				historyitemprice = $.toNumber(jsondata[jsonitem].price);
				if(jsondata[jsonitem].site=="amazon"&&historyitemprice==0){
            historyitemprice = '<a href="javascript:void(0)" class="redunderline">查看價格</a>';
				}else{
            historyitemprice += search_interface["_yen"];
          }
				addhtml = addhtml.replace(/##price#/g,historyitemprice);
				storename = jsondata[jsonitem].store;
				storename = (storename==null)?"":storename;
				if(storename.indexOf(".gif")!=-1){
					storename = (storename.indexOf(".gif")!=-1)?"<img src=\""+storename+"\">":storename;
				}
				else if(storename!=""&&storename!=null&&storename!=undefined){
					storename = '<img class="bg logos stone" src="/template/images/clear.gif" alt="">'+storename;
				}else{
          if(jsondata[jsonitem].sType=="shopping"){
            storename="<img src=\"/template/images/premium.gif\" alt=\"\" style=\"margin-top:4px;\"/>";
          }
				}
				addhtml = addhtml.replace(/##store#/g,storename);
				addhtml = addhtml.replace(/##itemkey#/g,jsondata[jsonitem].itemkey);
				itemendtime = jsondata[jsonitem].endtime;
				itemendtime = (itemendtime!=null&&itemendtime!="")?GetTime(itemendtime):"";
				addhtml = addhtml.replace(/##info#/g,itemendtime);
				addhtml = addhtml.replace(/##stype#/g,jsondata[jsonitem].sType);
				//$("#search_history").append(addhtml);
				jCarouselLiteulhtml+="<li class='jCarouselLiteli'>"+addhtml+"</li>";
			}
			//$(".jCarouselLiteul").append("<li class='jCarouselLiteli'>"+addhtml+"</li>");
		}
		if(""!=jCarouselLiteulhtml){
			$(".jCarouselLiteul").html(jCarouselLiteulhtml);
			var btn_n       = null;
			var btn_p       = null;
			if(realdata_length>0){
				$("#searchhistory").show();
			}
			var visible_len = realdata_length;
			if(visible_len>5){
				visible_len = 5;
				btn_n       = ".fjs_history_right";
				btn_p       = ".fjs_history_left";
			}
			$(".jCarouselLite").jCarouselLite({
				btnNext: btn_n,
				btnPrev: btn_p,
				visible :visible_len
		
			});
			checkimgload($("#search_history .item_main_images"),100,100);
		}
	};
	  reGetHistorytitle = function(site,stype,itemid,itemkey){
		querystring = "site="+site+"&type="+stype+"&op=item&itemid="+itemid+"&";
		$.ajax({
			type: "get",
			url: "./WebService/search.php?dom=JSON&"+querystring+Math.random()*10000000000,
			dataType: "json",
			success: function(msg){
				$("#search_history").find("#items_"+itemkey).find(".item_title").html(msg.title);
				store_obj = $("#search_history").find("#items_"+itemkey).find(".item_store");
				if(store_obj.html()==""){
					stonename = msg.store;
					if(stonename.indexOf("gif")>0){
						stonename = "<img src='"+stonename+"'/>";
					}
					else if(msg.store!=""&&msg.store!=null){
						stonename = '<img class="bg logos stone" src="/template/images/clear.gif" alt="">'+msg.store;
					}
					store_obj.html(stonename);
				}
				//alert(msg.title);
				//for(jsonitem in msg){
				//alert(msg[jsonitem].title);
				//$("#search_history").find("#items_"+itemkey).find(".item_title").html(msg[jsonitem].title);
				//}
			}
		});
	};
	/////////////////////////////////////////

})(jQuery);